package com.javaclimb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javaclimb.entity.Goods;
import com.javaclimb.entity.Orders;
import com.javaclimb.entity.OrdersDetail;
import com.javaclimb.mapper.GoodsMapper;
import com.javaclimb.mapper.OrdersDetailMapper;
import com.javaclimb.mapper.OrdersMapper;
import com.javaclimb.request.OrdersReq;
import com.javaclimb.resp.OrdersResponse;
import com.javaclimb.service.OrdersService;
import com.javaclimb.util.ReturnData;
import com.javaclimb.util.ReturnDataForLayui;
import com.javaclimb.util.UUIDUtil;
import com.javaclimb.util.UpdateOrInsertResultDeal;
import com.javaclimb.util.exception.CustomerException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private GoodsMapper goodsMapper;


    @Autowired
    private OrdersDetailMapper ordersDetailMapper;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    @Override
    public ReturnDataForLayui getList(Orders orders) {
        PageHelper.startPage(orders.getPage(), orders.getLimit());
        List<Orders> list = ordersMapper.getList(orders);

        List<OrdersResponse> collect = list.stream().map(x -> {
            OrdersResponse response = new OrdersResponse();
            BeanUtils.copyProperties(x, response);
            Integer goodsId = x.getGoodsId();
            Goods goods = goodsMapper.selectById(goodsId);
            if (Objects.nonNull(goods)) {
                response.setGoodsName(goods.getName());
            }
            return response;
        }).collect(Collectors.toList());
        PageInfo<Orders> info = new PageInfo<>(list);
        return ReturnDataForLayui.success(collect, info.getTotal());
    }

    @Override
    public ReturnData addOrder(@RequestBody OrdersReq req) {
        Orders orders = new Orders();
        //获取订单编号
        String orderSn = UUIDUtil.getOrderSn();
        orders.setTotalPrice(req.getTotalPrice());
        orders.setUid(req.getUid());
        orders.setNum(req.getNum());
        orders.setDayCount(req.getDayCount());
        orders.setGoodsId(req.getGoodsId());
        orders.setAddressId(req.getAddressId());
        orders.setOrderSn(orderSn);
        orders.setGoodsId(req.getGoodsId());
        orders.setExpireTime(getDateAfter(new Date(), req.getDayCount()));
        ordersMapper.add(orders);
        //扣减库存
        goodsMapper.updateCount(req.getGoodsId(), req.getNum());

        //保存订单详情
        return ReturnData.success(orders.getOrderSn());
    }


    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();

    }

    public static void main(String[] args) {
        System.out.println(getDateAfter(new Date(), 2));
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public ReturnData update(Orders orders) {
        int i = ordersMapper.update(orders);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Override
    public ReturnData updateState(Orders orders) {
        int i = ordersMapper.updateState(orders);
        return UpdateOrInsertResultDeal.dealWith(i);
    }

    @Override
    public ReturnData updateStatePay(Orders orders) {
        int i = ordersMapper.updateStatePay(orders);
        return UpdateOrInsertResultDeal.dealWith(i);
    }
}
